﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;

namespace Appsters
{
    public partial class JotFrame : Form
    {
        //OleDbConnection dbConn;
        public JotFrame()
        {
            InitializeComponent();
            string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                                "Jotter");

            if(Directory.Exists(path))
            {

                Console.WriteLine("File already exists.");
            }
            else
            {
                System.IO.Directory.CreateDirectory(path);
            }

            if ((pBox.Image == null) || (name.TextLength < 0) || (desc.TextLength < 0))
            {
                ok.Enabled = false;
            }

            if(pBox.Image == null)
            {
                name.Enabled = false;
                desc.Enabled = false;
            }

           // OleDbConnectionStringBuilder csb = new OleDbConnectionStringBuilder();

            //csb["provider"] = "Microsoft.ACE.OLEDB.12.0";
            //csb["data source"] = Application.StartupPath + @"\database\Jotter.l accdb";

            //string connStr = csb.ConnectionString;

            System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
            gp.AddEllipse(0, 0, pBox.Width, pBox.Height);
            Region rg = new Region(gp);
            pBox.Region = rg;
        }   


        private void exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void addPic_Click(object sender, EventArgs e)
        {
            string PIC = "";           
            
            if (open.ShowDialog() == DialogResult.OK)
            {
                PIC = open.FileName;
                pBox.Image = Image.FromFile(PIC);
                
            }

            System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
            gp.AddEllipse(0, 0, pBox.Width, pBox.Height);
            Region rg = new Region(gp);
            pBox.Region = rg;

            addPic.Hide();

            name.Enabled = true;

        }

        private void clear_Click(object sender, EventArgs e)
        {
            pBox.Image = null;
            name.Clear();
            desc.Clear();
            addPic.Show();
        }

        private void ok_Click(object sender, EventArgs e)
        {
            if (ok.Enabled == true)
            {
                string picPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string pathString = System.IO.Path.Combine(picPath, "Jotter\\Display Picture");
                System.IO.Directory.CreateDirectory(pathString);

                string picFileName = "Profile.jpeg";
                string completePath = System.IO.Path.Combine(pathString, picFileName);
                pBox.Image.Save(completePath, ImageFormat.Jpeg);

                string n = name.Text;
                string d = desc.Text;
                string[] det = new string[2];

                det[0] = n;
                det[1] = d;

                string namePath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                pathString = System.IO.Path.Combine(namePath, "Jotter\\Saved Files\\Log In Details");
                System.IO.Directory.CreateDirectory(pathString);

                string filename = "Name.txt";
                string filename2 = "Description.txt";

                string pathString1 = System.IO.Path.Combine(pathString, filename);

                string pathString2 = System.IO.Path.Combine(pathString, filename2);

                if (!System.IO.File.Exists(pathString1))
                {
                    using (System.IO.FileStream f = System.IO.File.Create(pathString1))
                    {
                        using (TextWriter t = new StreamWriter(f))
                        {
                            t.WriteLine(name.Text);
                            t.Close();
                        }
                        f.Close();
                    }
                }
                else
                {
                    using (System.IO.FileStream f = System.IO.File.OpenWrite(pathString1))
                    {
                        using (TextWriter t = new StreamWriter(f))
                        {
                            t.WriteLine(name.Text);
                            t.Close();
                        }
                        f.Close();
                    }
                }

                if (!System.IO.File.Exists(pathString2))
                {
                    using (System.IO.FileStream f = System.IO.File.Create(pathString2))
                    {
                        using (TextWriter t = new StreamWriter(f))
                        {
                            t.WriteLine(desc.Text);
                            t.Close();
                        }
                        f.Close();
                    }
                }
                else
                {
                    using (System.IO.FileStream f = System.IO.File.OpenWrite(pathString2))
                    {
                        using (TextWriter t = new StreamWriter(f))
                        {
                            t.WriteLine(desc.Text);
                            t.Close();
                        }
                        f.Close();
                    }
                }

                profView p = new profView();
                p.Show();
                this.Hide();
            }
         
        }

        private void min_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void name_TextChanged_1(object sender, EventArgs e)
        {
            ok.Enabled = true;
            desc.Enabled = true;
        }

        private void desc_TextChanged(object sender, EventArgs e)
        {
            if (desc.TextLength > 0)
            {
                ok.Enabled = true;
            }
        }
    }
}
